package com.hhhn.wk.main.tab4.mymoney;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hhhn.wk.R;
import com.hhhn.wk.base.BaseActivity;
import com.hhhn.wk.main.tab4.allorder.fragment.Order02Fragment;
import com.hhhn.wk.utils.AllPublic;
import com.hhhn.wk.utils.Constants;
import com.hhhn.wk.utils.mi.AES;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import static android.content.ContentValues.TAG;

/**
 * * * * * * * * * * * *
 * @author：文珂    ｡◕‿◕｡ *
 * Data：2018/3/29 0029    ｡◕‿◕｡ *
 * Function：      ｡◕‿◕｡ *
 * 我的余额
 * * * * * * * * * * * */
public class MyMoneyActivity extends BaseActivity {
    MBroadcastReceiver mBroadcastReceiver;//广播接受者
    private TextView tv_title;
    BaseActivity activity;
    Context context;
    private TextView tv_money;
    private Button btn_ok;
    private LinearLayout ll_mingx;
    @Override
    protected void setOnCreateContentView() {
        setContentView(R.layout.activity_my_money);
        activity=this;
        context=MyMoneyActivity.this;
    }
    @Override
    protected void initView() {
        tv_money=findViewById(R.id.tv_money);
        tv_title=findViewById(R.id.tv_title);
        btn_ok=findViewById(R.id.btn_ok);
        ll_mingx=findViewById(R.id.ll_mingx);
        tv_title.setText("余额");


        mBroadcastReceiver = new MBroadcastReceiver();
        IntentFilter intentFilter = new IntentFilter();
        //设置接收广播的类型
        intentFilter.addAction(Constants.MBroadcastReceiverRecharge);
        //调用Context的registerReceiver（）方法进行动态注册
        activity.registerReceiver(mBroadcastReceiver, intentFilter);
    }
    @Override
    protected void initData() {
        getJSON();
    }
    @Override
    protected void initClick() {
        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context,RechargeActivity.class);

                startActivity(intent);
            }
        });
        ll_mingx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context,DetailActivity.class);

                startActivity(intent);
            }
        });
    }
    //获取余额
    public void getJSON() {
        showLoad();
        RequestParams params = AllPublic.getParamsAccount(activity, "/queryBalance", false);
        try {
            JSONObject person=new JSONObject();
            person.put("merchantId",getUser().getUid());
            AES aes = AllPublic.jsonDesPackaging(person);
            params.addParameter("data",aes.getData());
            params.addParameter("key",aes.getKey());
        } catch (JSONException e) {
            e.printStackTrace();
        }
        //请求
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override  //成功
            public void onSuccess(String decode) {
                String result = AllPublic.jsonDesDecode(decode);
                try {
                    JSONObject jsonObject= new JSONObject(result);
                    String reason = jsonObject.getString("code");
                    if (reason.equals("0")) {
                        String data = jsonObject.getString("data");
                        tv_money.setText(data);
                    }else {
                        toastWk(jsonObject.getString(getResources().getString(R.string.errMsg)));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                dismissLoad();
            }
            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Log.d("获取余额-第二个", ex + "");
                ex.printStackTrace();
                dismissLoad();
            }
            @Override
            public void onCancelled(CancelledException cex) {
                Log.d("获取余额-第三个", cex + "");
                dismissLoad();
            }
            @Override
            public void onFinished() {
                Log.d("获取余额-第四个", "onFinished");
                dismissLoad();
            }
        });
    }
    public class MBroadcastReceiver extends BroadcastReceiver {
        //接收到广播后自动调用该方法
        @Override
        public void onReceive(Context context, Intent intent) {
            //写入接收广播后的操作
            Log.d(TAG, "MBroadcastReceiver: 待付款接收广播");
//            handler.sendEmptyMessage(1);
            getJSON();
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        activity.unregisterReceiver(mBroadcastReceiver);
    }
}
